package reflect_t

import (
	"fmt"
	"reflect"
)

type student struct {
	Name string "学生姓名"
	Age  int    `a:"111"b:"2222"`
}

func ref1() {
	s := student{}
	rt := reflect.TypeOf(s)

	fieldName, ok := rt.FieldByName("Name")
	if ok {
		fmt.Println(fieldName.Tag)
	}

	fieldAge, ok := rt.FieldByName("Age")
	if ok {
		fmt.Println(fieldAge.Tag.Get("a"), fieldAge.Tag.Get("b"))
	}

	fmt.Println("Name:", rt.Name())
	fmt.Println("NumField:", rt.NumField())
	fmt.Println("PkgPath:", rt.PkgPath())
	fmt.Println("String:", rt.String())

	println("=============================")
	fmt.Println("kind_string:", rt.Kind().String())
	fmt.Println("type.String:", rt.String())

	for i := 0; i < rt.NumField(); i++ {
		fmt.Printf("type.Field(%d).Name = [%v] \n", i, rt.Field(i).Name)
	}

	sc := make([]int, 10)
	sc = append(sc, 1, 2, 3)
	sct := reflect.TypeOf(sc)

	scet := sct.Elem()

	fmt.Println("slice element type.Kind()", scet.Kind())
	fmt.Printf("slice element type.Kind()= %d\n", scet.Kind())
	fmt.Println("slice element type.String()", scet.String())
	fmt.Println("slice element type.Name()", scet.Name())
	fmt.Println("slice element type.NumMethod()", scet.NumMethod())
	fmt.Println("slice element type.PkgPath()", scet.PkgPath())
	fmt.Println("slice element type.PkgPath()", scet.PkgPath())
}

type INT int
type A struct {
	a int
}

type B struct {
	b string
}

type Ita interface {
	String() string
}

func (b B) String() string {
	return b.b
}

func ref2() {
	var a INT = 12
	var b int = 12

	ta := reflect.TypeOf(a)
	tb := reflect.TypeOf(b)

	if ta == tb {
		println("a == b")
	} else {
		println("ta != tb")
	}

	println(ta.Name(), "\t", tb.Name())
	println(ta.Kind().String(), "\t", tb.Kind().String())

	s1 := A{a: 1}
	s2 := B{b: "tata"}

	println(reflect.TypeOf(s1).Name(), reflect.TypeOf(s2).Name())
	println(reflect.TypeOf(s1).Kind().String(), "----", reflect.TypeOf(s2).Kind().String())

	ita := new(Ita)
	//返回接口信息
	println(reflect.TypeOf(ita).Elem().Name(), reflect.TypeOf(ita).Elem().Kind().String())
	var itb = s2
	//返回struct信息
	println(reflect.TypeOf(itb).Name(), reflect.TypeOf(itb).Kind().String())
}
